package GameOntology;



import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class GameWindow extends JFrame {
    private final int width = 971;
    private final int height = 820;
    private int status = 0;//游戏窗口界面
    private int pointerLocation=210;//菜单选择指针
    private int a = 1;//菜单的中间量
    GameWindow me = this;
    Random random = new Random();
    private int life = 2;
    private int computerNum=0;
    private int upperLimit=0;
    private int base ;
    ArrayList<Player> players = new ArrayList<>();
    ArrayList<Bullet> bullets = new ArrayList<>();
    ArrayList<ComputerPlayer> computerPlayers = new ArrayList<>();
    ArrayList<Wall> walls = new ArrayList<>();
//    ArrayList<Base> bases = new ArrayList<>();
    ArrayList<Bullet> removeBullets = new ArrayList<>();
    ArrayList<Wall> removeWall = new ArrayList<>();
//    ArrayList<Base> removeBase = new ArrayList<>();


    public void setBase(int base) {
        this.base = base;
    }

    public void addPlayer(){
        if (players.isEmpty()){
            players.add(new Player("game\\img\\tank1_up.png",300,723,"game/img/tank1_up.png","game/img/tank1_left.png",
                    "game/img/tank1_right.png","game/img/tank1_down.png",this));
        }else if (!players.isEmpty()){
            players.get(0).setX(300);
            players.get(0).setY(723);
            players.get(0).setImg("game\\img\\tank1_up.png");
        }

    }

    public void cartography (){
        String wallImg = "game/img/wall.gif";
        for (int i = 0; i < 7; i++) {
            walls.add(new Wall(wallImg,0,120+60*i,this));
            walls.add(new Wall(wallImg,901,120+60*i,this));
            walls.add(new Wall(wallImg,240,120+60*i,this));
            walls.add(new Wall(wallImg,660,120+60*i,this));
        }
        for (int i = 0; i < 7; i++) {
            walls.add(new Wall(wallImg,240+60*i,240,this));
            walls.add(new Wall(wallImg,240+60*i,300,this));
            walls.add(new Wall(wallImg,240+60*i,360,this));
        }
        for (int i = 0; i < 3; i++) {
            walls.add(new Wall(wallImg,360,723-60*i,this));
            walls.add(new Wall(wallImg,540,723-60*i,this));
        }
        for (int i = 0; i < 2; i++) {
            walls.add(new Wall(wallImg,420+60*i,603,this));
            walls.add(new Wall(wallImg,420+60*i,663,this));
        }
        walls.add(new Wall("game/img/home.gif",480,723,this));
        base=1;
    }
    private int b=0;
    public void addComputer(){
        int []arrX = {60,750,500};
        int x =arrX[random.nextInt(3)];

        Rectangle rectangle = new Rectangle(x,20,60,60);
        while (true){
            int a = 0;
            for (ComputerPlayer player : computerPlayers) {
                if (player.getRec().intersects(rectangle)) {
                    x=arrX[random.nextInt(2)];
                    a=1;
                }
            }
            if (a==1&&b==0){
                b=1;
                continue;
            } else if (a==1&&b==1){
                return;
            }
            break;
        }
        if (computerPlayers.size()<10&&status==1&&upperLimit!=1){
            ComputerPlayer computerPlayer = new ComputerPlayer("game/img/tank2_down.png",x
                    ,0,
                    "game/img/tank2_up.png","game/img/tank2_left.png","game/img/tank2_right.png",
                    "game/img/tank2_down.png",me);
            computerPlayers.add(computerPlayer);
            computerNum++;
        }
        if (computerNum==10){
            upperLimit=1;
        }
    }

    public GameWindow() {
        initWindow();
        this.addKeyListener(new keyboard());
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                refreshInterface();
                if (players.isEmpty()&&life>0){
                    addPlayer();
                    life--;
                }
            }
        };
        //开线程刷新屏幕以解决坦克移动卡顿问题(部分未解决)
        timer.scheduleAtFixedRate(task,0,20);


        //开第二挑线程来解决坦克射速问题
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (!players.isEmpty()){
                    players.get(0).attack();
                }

                if (!computerPlayers.isEmpty()){
                    for (ComputerPlayer computerPlayer : computerPlayers) {
                        computerPlayer.randomAttack();
                    }
                }
            }
        },0,300);

        //第三个线程:检测并增加电脑坦克
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                addComputer();

            }
        },2000,3000);

        //第四个线程:给每一辆电脑坦克实时设置移动参数
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                if (!computerPlayers.isEmpty()){
                    for (int i = 0; i < computerPlayers.size(); i++) {
                        computerPlayers.get(i).randomMove_argument();
                    }
                }
            }
        },0,2000);
    }


    //刷新窗口状态
    private void refreshInterface() {

        this.getContentPane().removeAll();
        if (status==0){
            gameStartMenu();

        } else if (status == 1){
            if (!players.isEmpty()){
                players.get(0).keyMove();
            }
            if ((players.isEmpty()&&life==0)||base==0){
                status=3;
                computerPlayers.clear();
                walls.clear();
                bullets.clear();
                upperLimit=1;
                return;
            }
            if (!walls.isEmpty()){
                for (int i = 0; i < walls.size(); i++) {
                    walls.get(i).beHit();
                }
            }
            if (!removeWall.isEmpty()){
                walls.removeAll(removeWall);
            }
            if (!bullets.isEmpty()){
                for (int i = 0; i < bullets.size(); i++) {
                    bullets.get(i).hit();
                    bullets.get(i).move();
                }
            }
            if (!computerPlayers.isEmpty()){
                for (int i = 0; i < computerPlayers.size(); i++) {
                    computerPlayers.get(i).randomMove();
                }
            }
            if (computerPlayers.isEmpty()&&upperLimit==1&&!players.isEmpty()){
                status=2;
                walls.clear();
                bullets.clear();
                return;
            }
            if (!removeBullets.isEmpty()){
                bullets.removeAll(removeBullets);
            }
            gameInterface();
        } else if (status ==2) {
            gameWinPicture();
        } else if (status ==3){
            gameFailPicture();
        }
        this.getContentPane().repaint();

    }

    //游戏进行界面
    private void gameInterface(){
        JPanel display = new JPanel();
        display.setBounds(0,0,width,height);

        if (!players.isEmpty()){
            JLabel playerLabel = new JLabel(players.get(0).getImg());
            playerLabel.setBounds(players.get(0).getX(),players.get(0).getY(),
                    players.get(0).getWidth(),players.get(0).getHeight());
            display.add(playerLabel);
        }


        if (!walls.isEmpty()){
            for (int i = 0; i < walls.size(); i++) {
                JLabel wallLabel = new JLabel(walls.get(i).getImg());
                wallLabel.setBounds(walls.get(i).getX(),walls.get(i).getY(),
                        walls.get(i).getWidth(),walls.get(i).getHeight());
                display.add(wallLabel);
            }

        }


        if (!computerPlayers.isEmpty()){
            for (int i = 0; i < computerPlayers.size(); i++) {
                JLabel computer = new JLabel(computerPlayers.get(i).getImg());
                computer.setBounds(computerPlayers.get(i).getX(),computerPlayers.get(i).getY(),
                        computerPlayers.get(i).getWidth(),computerPlayers.get(i).getHeight());
                display.add(computer);
            }
        }

        if (!bullets.isEmpty()){
            for (Bullet value : bullets) {
                JLabel bullet = new JLabel(value.getImg());
                bullet.setBounds(value.getX(), value.getY(),
                        value.getWidth(), value.getHeight());
                display.add(bullet);
            }

        }

        JLabel bgi = new JLabel(new ImageIcon("game\\img\\background2.jpg"));
        bgi.setBackground(Color.BLACK);
        bgi.setBounds(0,0,width,height);
        display.add(bgi);
        this.add(display);
    }

    //游戏初始化界面渲染
    private void gameStartMenu() {
        JPanel bg = new JPanel();
        bg.setBounds(0,0,width+10,height+10);

        JLabel title = new JLabel("坦克大战");
        title.setFont(new Font("宋体", Font.BOLD, 35));
        title.setForeground(Color.blue);
        title.setBounds(width/3+80,height/3,300,50);
        bg.add(title);

        JLabel start = new JLabel("开始游戏");
        start.setFont(new Font("宋体", Font.BOLD, 25));
        start.setForeground(Color.red);
        start.setBounds(width/3+100,height/3+200,300,50);
        bg.add(start);

        JLabel end = new JLabel("退出");
        end.setFont(new Font("宋体", Font.BOLD, 25));
        end.setForeground(Color.red);
        end.setBounds(width/3+125,height/3+260,300,50);
        bg.add(end);


        JLabel pointer = new JLabel(new ImageIcon("game\\img\\tank1_right.png"));
        pointer.setBounds(width/3+50,height/3+pointerLocation,30,30);
        bg.add(pointer);


        JLabel bgi = new JLabel(new ImageIcon("game\\img\\background1.jpg"));
        bgi.setBounds(0,0,width,height);
        bg.add(bgi);
        this.add(bg);
    }

    public void gameWinPicture(){
        JPanel bg = new JPanel();
        bg.setBounds(0,0,width,height);

        JLabel title = new JLabel("获胜!!");
        title.setFont(new Font("宋体", Font.BOLD, 60));
        title.setForeground(Color.blue);
        title.setBounds(width/3+50,height/3-20,300,50);
        bg.add(title);

        JLabel start = new JLabel("继续");
        start.setFont(new Font("宋体", Font.BOLD, 25));
        start.setForeground(Color.red);
        start.setBounds(width/3+120,height/3+200,300,50);
        bg.add(start);

        JLabel end = new JLabel("退出");
        end.setFont(new Font("宋体", Font.BOLD, 25));
        end.setForeground(Color.red);
        end.setBounds(width/3+120,height/3+260,300,50);
        bg.add(end);

        JLabel pointer = new JLabel(new ImageIcon("game\\img\\tank1_right.png"));
        pointer.setBounds(width/3+70,height/3+pointerLocation,30,30);
        bg.add(pointer);

        JLabel bgi = new JLabel(new ImageIcon("game\\img\\background2.jpg"));
        bgi.setBounds(0,0,width,height);
        bg.add(bgi);
        this.add(bg);
    }

    public void gameFailPicture(){
        JPanel bg = new JPanel();
        bg.setBounds(0,0,width,height);

        JLabel title = new JLabel("失败");
        title.setFont(new Font("宋体", Font.BOLD, 60));
        title.setForeground(Color.blue);
        title.setBounds(width/3+90,height/3-20,300,50);
        bg.add(title);

        JLabel start = new JLabel("重新开始");
        start.setFont(new Font("宋体", Font.BOLD, 25));
        start.setForeground(Color.red);
        start.setBounds(width/3+95,height/3+200,300,50);
        bg.add(start);

        JLabel end = new JLabel("退出");
        end.setFont(new Font("宋体", Font.BOLD, 25));
        end.setForeground(Color.red);
        end.setBounds(width/3+120,height/3+260,300,50);
        bg.add(end);

        JLabel pointer = new JLabel(new ImageIcon("game\\img\\tank1_right.png"));
        pointer.setBounds(width/3+50,height/3+pointerLocation,30,30);
        bg.add(pointer);

        JLabel bgi = new JLabel(new ImageIcon("game\\img\\background2.jpg"));
        bgi.setBounds(0,0,width,height);
        bg.add(bgi);
        this.add(bg);
    }

    //初始化窗口
    private void initWindow() {
        this.setSize(width,height);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setTitle("坦克大战");
        this.setLocationRelativeTo(null);
        this.setLayout(null);
        refreshInterface();
        this.setVisible(true);
    }


    //监听键盘事件
    class keyboard implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {
            //菜单选择
            switch (e.getKeyChar()){
                case '1':
                    pointerLocation=210;
                    a = 1;
                    refreshInterface();
                    break;
                case '2':
                    pointerLocation=270;
                    a = -1;
                    refreshInterface();
                    break;
                case '\n':
                    if (a<0){
                        System.exit(0);
                    }
                    status = a;
                    if (status==1){
                        life=2;
                        upperLimit=0;
                        computerNum=0;
                        addPlayer();
                        cartography();
                    }
                    refreshInterface();
                    break;
            }
        }

        @Override
        public void keyPressed(KeyEvent e) {
            if (!players.isEmpty()){
                players.get(0).keyPressed(e);
            }

        }
        @Override
        public void keyReleased(KeyEvent e) {
            if (!players.isEmpty()){
                players.get(0).keyReleased(e);
            }
        }
    }
}
